package com.se2.antiqueexhibition.Controller;



import com.se2.antiqueexhibition.Service.AdminNewsService;
import com.se2.antiqueexhibition.pojo.AdminNews;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/News")
public class AdminNewsController {

    @Resource
    private AdminNewsService adminNewsService;

    @GetMapping("/list")
    public String list(Model model) {
        List<AdminNews> adminNews = adminNewsService.getAllNews();
        model.addAttribute("adminNews", adminNews);
        return "news-management";
    }

    @PostMapping("/add")
    public String add(@ModelAttribute AdminNews adminNews, Model model) {
        try{
            adminNewsService.insert(adminNews);
            model.addAttribute("adminNews", adminNews);
            model.addAttribute("message", "资讯添加成功");
            model.addAttribute("success",true);
            return "redirect:list";
        }catch(Exception e){

            model.addAttribute("message","资讯添加失败");
            model.addAttribute("success",false);
            model.addAttribute("adminNews", adminNews);
            return "news-from";
        }

    }

    @PostMapping("/update")
    public String update(@ModelAttribute AdminNews adminNews, Model model) {
        try{
            adminNewsService.update(adminNews);
            model.addAttribute("adminNews", adminNews);
            model.addAttribute("message","资讯更新成功");
            model.addAttribute("success",true);
            return "redirect:list";
        }catch(Exception e){
            model.addAttribute("message","资讯更新失败");
            model.addAttribute("success",false);
            model.addAttribute("adminNews", adminNews);
            return "news-from";
        }
    }

    @GetMapping("/delete")
    public String delete(@RequestParam int id, Model model) {
        try{
            adminNewsService.delete(id);
            model.addAttribute("message","资讯删除成功");
            model.addAttribute("success",true);
        }catch(Exception e){
            model.addAttribute("message","资讯删除失败");
            model.addAttribute("success",false);

        }
            return "redirect:list";
    }



}
